import fs from 'fs';
import path from 'path';

/**
 * 读取 package.json 信息并判断是否为前端工程
 * @returns {Promise<{ isFrontend: boolean, name: string, version: string, description: string }>}
 */
async function readPackageInfo() {
    return new Promise((resolve, reject) => {
        const packageJsonPath = path.join(process.cwd(), 'package.json');
        fs.readFile(packageJsonPath, 'utf8', (err, data) => {
            if (err) {
                console.error('读取 package.json 文件时出错:', err);
                reject(err);
                return;
            }

            try {
                const packageInfo = JSON.parse(data);
                const { name, version, description, dependencies = {}, devDependencies = {} } = packageInfo;
                const allDependencies = { ...dependencies, ...devDependencies };
                const isFrontend = ['vue', 'react', 'vite'].some(dep => allDependencies[dep]);

                resolve({
                    isFrontend,
                    name,
                    version,
                    description
                });
            } catch (parseError) {
                console.error('解析 package.json 文件时出错:', parseError);
                reject(parseError);
            }
        });
    });
}

export default readPackageInfo;